package com.qf.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qf.pojo.*;
import com.qf.service.*;
import com.qf.util.ResponseUtil;
import com.qf.vo.GoodsAllinone;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @version v1.0.0
 * @Description:
 * @author: 姜红凯
 * @date: 2022.02.23
 * @since 1.0
 */
@RestController
@RequestMapping("/admin/goods")
public class DtsGoodsController {

    @Autowired
    DtsBrandService brandService;

    @Autowired
    DtsGoodsService goodsService;

    @Autowired
    DtsCategoryService categoryService;

    @Autowired
    DtsGoodsAttributeService goodsAttributeService;

    @Autowired
    DtsGoodsSpecificationService goodsSpecificationService;

    @Autowired
    DtsGoodsProductService goodsProductService;

    @RequestMapping("/create")
    public Object create(@RequestBody GoodsAllinone goodsAllinone){
        return goodsService.create(goodsAllinone);

    }

    @RequestMapping("/delete")
    public Object delete( @RequestBody DtsGoods goods){
        return goodsService.deleteById(goods.getId());
    }

    @RequestMapping("/update")
    public Object update(@RequestBody GoodsAllinone goodsAllinone){
        return goodsService.update(goodsAllinone);

    }

    @RequestMapping("/catAndBrand")
    public Object catAndBrand(){
        //查询分类列表
        //查询一级分类
        List<DtsCategory> categoryList = categoryService.queryL1();

        //把categoryList 转换为前端需要的格式
        List<Map<String,Object>> catList = new ArrayList<>();

        //循环 分类列表，每个分类对应一个Map集合
        for (DtsCategory category : categoryList) {
            Map<String,Object> map = new HashMap<>();
            //分类id
            map.put("value",category.getId());
            //分类名称
            map.put("label",category.getName());
            //子（二级）分类
            //根据一级分类id查询二级分类
            List<DtsCategory> categoryListL2 =  categoryService.findL2sByL1(category.getId());
            List<Map<String,Object>> catsL2 = new ArrayList<>();
            //循环二级分类列表
            for (DtsCategory catL2 : categoryListL2) {
                Map<String,Object> map2 = new HashMap<>();
                //分类id
                map2.put("value",catL2.getId());
                //分类名称
                map2.put("label",catL2.getName());

                catsL2.add(map2);
            }


            map.put("children",catsL2);

            catList.add(map);

        }

        List<Integer> brandIds = null;
        //查询品牌列表
        Subject subject = SecurityUtils.getSubject();
        if(subject.hasRole("品牌制造商")){
           DtsAdmin admin = (DtsAdmin) subject.getPrincipal();

            brandIds = brandService.getBrandIdsByAdminId(admin.getId());
        }
       List<DtsBrand>  brands = brandService.selectList(brandIds);
        List<Map<String, Object>> brandList = brands.stream().map(brand -> {
            Map<String, Object> map = new HashMap<>();
            map.put("value", brand.getId());
            map.put("label", brand.getName());
            return map;
        }).collect(Collectors.toList());

        Map<String,Object> data = new HashMap<>();
        data.put("categoryList",catList);


        data.put("brandList",brandList);

        return ResponseUtil.ok(data);
    }
    /**
     *
     * @param id
     * @return
     */
    @RequestMapping("/detail")
    public Object detail(Integer id){

        //查询商品的信息
        DtsGoods goods = goodsService.findById(id);

        //查询分类id(大分类，小分类)
        Integer L2 = goods.getCategoryId();
        //根据二级分类id查询一级分类id
        Integer L1 = categoryService.findL1ByL2(L2);

        //获取属性
        //根据商品id得到商品信息
        List<DtsGoodsAttribute> attributes = goodsAttributeService.findByGoodsId(id);
        List<DtsGoodsSpecification>specifications = goodsSpecificationService.findByGoodsId(id);
        List<DtsGoodsProduct> products = goodsProductService.findById(id);


        Map<String ,Object> data = new HashMap<>();
        data.put("goods",goods);
        data.put("categoryIds", Arrays.asList(L1,L2));
        data.put("attributes", attributes);
        data.put("specifications", specifications);
        data.put("products", products);


        return ResponseUtil.ok(data);

    }


    @GetMapping("/list")
    public Object list(
            @RequestParam(value = "page",required = false,defaultValue = "1") Integer page ,
            @RequestParam(value = "limit",required = false,defaultValue = "10")Integer limit ,
            String goodsSn , String name,String sort , String order) {

        //如果是品牌制造商，只能查询相关品牌数据
        Subject subject = SecurityUtils.getSubject();
        DtsAdmin admin = (DtsAdmin) subject.getPrincipal();

        List<Integer> brandIds = null;

        if(subject.hasRole("品牌制造商")) {
            //根据adminId查询相关品牌信息
            brandIds = brandService.getBrandIdsByAdminId(admin.getId());
        }
          IPage<DtsGoods> iPage =  goodsService.queryPage(page,limit,goodsSn,name,sort,order,brandIds);

        //创建结果对象
        Map<String,Object> data = new HashMap<>();
        data.put("items",iPage.getRecords());
        data.put("total",iPage.getTotal());

        return ResponseUtil.ok(data);
    }
}
